package br.edu.ufcg.dsc.domain.resource;

import java.util.ArrayList;
import java.util.List;

import br.edu.ufcg.dsc.domain.marc.MARCResource;

public class Book {

	private String isbn, author, title, place, publisher, summary;

	private List<String> subjects, collaborator;

	public Book() {
		subjects = new ArrayList<String>();
		collaborator = new ArrayList<String>();
	}

	public Book(MARCResource marc) {
		setIsbn(getAttribute(marc, "020", "a"));
		setAuthor(getAttribute(marc, "100", "a"));
		setTitle(getAttribute(marc, "245", "a"));
		setPlace(getAttribute(marc, "260", "a"));
		setPublisher(getAttribute(marc, "260", "b"));
		setSummary(getAttribute(marc, "520", "a"));

		subjects = getMultipleAttributes(marc, "650", "a");
		collaborator = getMultipleAttributes(marc, "700", "a");
	}

	private List<String> getMultipleAttributes(MARCResource marc, String field,
			String subfield) {

		List<String> result = new ArrayList<String>();

		for (String key : marc.getDataFields().keySet())
			if (key.startsWith(field))
				for (String subkey : marc.getDataFields().get(key)
						.getSubfields().keySet())
					if (subkey.equals(subfield))
						result.add(marc.getDataFields().get(key)
								.getSubfields().get(subkey));

		return result;
	}

	private String getAttribute(MARCResource marc, String field, String subfield) {
		for (String key : marc.getDataFields().keySet())
			if (key.startsWith(field))
				for (String subkey : marc.getDataFields().get(key)
						.getSubfields().keySet())
					if (subkey.equals(subfield))
						return marc.getDataFields().get(key).getSubfields().get(subkey);
		return "";
	}

	public String getIsbn() {
		return isbn;
	}

	public void setIsbn(String isbn) {
		this.isbn = isbn;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getPlace() {
		return place;
	}

	public void setPlace(String place) {
		this.place = place;
	}

	public String getPublisher() {
		return publisher;
	}

	public void setPublisher(String publisher) {
		this.publisher = publisher;
	}

	public String getSummary() {
		return summary;
	}

	public void setSummary(String summary) {
		this.summary = summary;
	}

	public List<String> getSubjects() {
		return subjects;
	}

	public void setSubjects(List<String> subjects) {
		this.subjects = subjects;
	}

	public List<String> getCollaborator() {
		return collaborator;
	}

	public void setCollaborator(List<String> collaborator) {
		this.collaborator = collaborator;
	}

}
